<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Transactions : DataMapper User Guide</title>

<style type='text/css' media='all'>@import url('../css/userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../css/userguide.css' />

<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />

</head>

<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top">&nbsp;</a><a id="nav_toggle" href="#"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>DataMapper</h1></td>
<td id="breadcrumb_right"><a href="toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->

<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://stensi.com/">DataMapper Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
Validate
</td>
</tr>

</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>Transactions</h1>

<p>DataMapper handles <dfn>transactions</dfn> in very much the same way that CodeIgniter does (read CodeIgniter <a href="http://codeigniter.com/user_guide/database/transactions.html">Transactions</a>), obviously because it uses the same methods!  The only real difference is that you'll be calling the transaction methods directly on your DataMapper objects. For example:</p>

<code>
// Create user<br />
$u = new User();<br />
<br />
// Populate with form data<br />
$u->username = $this->input->post('username');<br />
$u->email = $this->input->post('email');<br />
$u->password = $this->input->post('password');<br />
$u->confirm_password = $this->input->post('confirm_password');<br />
<br />
// Begin transaction<br />
$u->trans_begin();<br />
<br />
// Attempt to save user<br />
$u->save();<br />
<br />
// Check status of transaction<br />
if ($u->trans_status() === FALSE)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;// Transaction failed, rollback<br />
&nbsp;&nbsp;&nbsp;&nbsp;$u->trans_rollback();<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;// Add error message<br />
&nbsp;&nbsp;&nbsp;&nbsp;$u->error_message('transaction', 'The transaction failed to save (insert)');<br />
}<br />
else<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;// Transaction successful, commit<br />
&nbsp;&nbsp;&nbsp;&nbsp;$u->trans_commit();<br />
}<br />
<br />
// Show all errors<br />
echo $u->error->string;
<br />
// Or just show the transaction error we manually added<br />
echo $u->error->transaction;<br />
</code>

<p>If you'd prefer not to have to write the extra transaction code, but would still like to use transactions, then you're in luck!  DataMapper has a <a href="configuration.html">configuration setting</a> called <kbd>auto_transaction</kbd> which, when set to TRUE, will automatically wrap your save and delete calls in transactions, even going so far as to give you an error message if the transaction was rolled back.</p>

<p>So, instead of the above, you can do the following and get the same result (provided you've got <kbd>auto_transaction</kbd> set to TRUE of course):</p>

<code>
// Create user<br />
$u = new User();<br />
<br />
// Populate with form data<br />
$u->username = $this->input->post('username');<br />
$u->email = $this->input->post('email');<br />
$u->password = $this->input->post('password');<br />
$u->confirm_password = $this->input->post('confirm_password');<br />
<br />
// Attempt to save user<br />
if ($u->save())<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;// Saved successfully<br />
}<br />
else<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;// Show all errors<br />
&nbsp;&nbsp;&nbsp;&nbsp;echo $u->error->string;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;// Or just show the transaction error<br />
&nbsp;&nbsp;&nbsp;&nbsp;echo $u->error->transaction;<br />
}<br />
<br />

</code>


</div>
<!-- END CONTENT -->


<div id="footer">
<p>
Previous Topic:&nbsp;&nbsp;<a href="validation.html">Validation</a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href="get.html">Get</a>
</p>
<p><a href="http://stensi.com">DataMapper</a> &nbsp;&middot;&nbsp; Copyright &#169; 2008 &nbsp;&middot;&nbsp; <a href="http://stensi.com/">Simon Stenhouse</a></p>
<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
</div>

<script type="text/javascript" src="../js/mootools.js"></script>
<script type="text/javascript" src="../js/menu.js"></script>
<script type="text/javascript">
<!--
	window.addEvent('domready', function() {

		// Create Menu
		var menu = new Menu({
			basepath: '../',
			pagepath: ''
		});

	});	
//-->
</script>
</body>
</html>